let oInput = document.querySelectorAll("input");
let oSpan = document.querySelectorAll("span");
let oDsan = document.querySelector(".dsan");
let oFang = document.querySelector(".fang");

console.log(oInput);
console.log(oSpan);
console.log(oFang);
oDsan.onclick = function () {
  oFang.style.backgroundColor = "black";
  oFang.style.color = "white";
};

// oDsan.onmouseout = function () {
//   oFang.style.backgroundColor = "";
//   oFang.style.color = "";
// };

// 输入手机号码
let flagPhone = false;
oInput[0].onblur = function () {
  let regPhone =
    /^1[3|4|5|7|8][0-9]{9}$/;
  if (regPhone.test(this.value)) {
    flagPhone = true;
    oSpan[1].innerHTML = "输入正确";
  } else {
    flagPhone = false;
    oSpan[1].innerHTML = "请输入有效的电子邮箱/手机号码";
  }
};

// 输入密码（8-12位数字字母组合）

let flagPassword = false;
oInput[1].onblur = function () {
  let regPassword = /^[A-Za-z0-9]{8,12}$/;
  if (regPassword.test(this.value)) {
    oSpan[2].innerHTML = "格式正确";
    flagPassword = true;
  } else {
    oSpan[2].innerHTML = "格式错误";
    flagPassword = false;
  }
};

// 请输入随机验证码
function rand(min, max) {
  return Math.round(Math.random() * (max - min) + min);
}
var num = "";
var str = "0123456789abcdef";
for (var i = 0; i < 4; i++) {
  num += str.charAt(rand(0, 15));
}
oSpan[3].innerHTML = num;

function randomCode() {
  var num = "";
  var str = "0123456789abcdef";
  for (var i = 0; i < 4; i++) {
    num += str.charAt(rand(0, 15));
  }
  console.log(num);
  oSpan[3].innerHTML = num;
}

oSpan[3].onclick;

let flagYz = false;
oInput[2].onblur = function () {
  if (oInput[2].value == oSpan[3].innerHTML) {
    flagYz = true;
    oSpan[4].innerHTML = "";
  } else {
    oSpan[4].innerHTML = "验证码错误";
    flagYz = false;
  }
};




// 点击注册让手机号和密码是正确的
// oFang.onclick = function () {
//   if (flagPhone == true && flagPassword == true) {
//     let xhr = new XMLHttpRequest();
//     xhr.open("post", "../../login.php", true);
//     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//     xhr.send(`username=${oInput[0].value}&userpass=${oInput[1].value}`);
//     xhr.onreadystatechange = function () {
//       if (xhr.readyState == 4 && xhr.status == 200) {
//         console.log(xhr.responseText);
//         fun(xhr.responseText);
//       }
//     };
//   } else {
//     alert("请输入正确的格式")
//   }
// }
// function fun(text) {
//   console.log(text);
// }




oFang.onclick = function (){
  let xhr = new XMLHttpRequest();
  xhr.open("post", "../../login.php", true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) 
      console.log(xhr.responseText);
      if(xhr.responseText=="﻿success"){
        location.href="http://127.0.0.1/2203/goodsAndShoppingCart/KW/kuangwei/banner.html";
        // console.log("成功");
        alert("chenggong");
      }else{
        console.log("失败");
      }
  }
  // xhr.open("post", "../../login.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

let str="username="+oInput[0].value+"&userpass="+oInput[1].value;
// console.log(oInput[0].value);
// console.log(oInput[1].value);
// console.log(str);
xhr.send(str);
}

